package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.SetMealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetMealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@Service(version = "1.0.0", interfaceClass = SetMealService.class)
@Transactional
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetMealDao setMealDao;

    @Override
    public Result findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<Setmeal> setmealPage = setMealDao.findPage(queryPageBean.getQueryString());
        PageResult pageResult = new PageResult();
        pageResult.setRows(setmealPage.getResult());
        pageResult.setTotal(setmealPage.getTotal());
        return Result.success(pageResult);
    }

    @Override
    public Result findAll() {
        List<CheckGroup> checkGroupList = setMealDao.findAll();
        return Result.success(checkGroupList);
    }

    @Override
    public Result findById(Integer id) {
        Setmeal setmeal = setMealDao.findById(id);
        List<CheckGroup> checkGroupList = setMealDao.findAll();
        List<Integer> checkGroupIds = setMealDao.findCheckGroupAndSetMeal(id);
        final HashMap<String, Object> map = new HashMap<>();
        map.put("fromData", setmeal);
        map.put("tableData", checkGroupList);
        map.put("checkGroupIds", checkGroupIds);
        map.put("url","http://qyymngysq.hb-bkt.clouddn.com/"+setmeal.getImg());
        return Result.success(map);
    }

    @Override
    public Result add(Setmeal setmeal) {
        setMealDao.add(setmeal);
        List<Integer> checkGroupIds = setmeal.getCheckGroupIds();
        for (Integer checkGroupId : checkGroupIds) {
            setMealDao.addSetMealAndCheckGroup(setmeal.getId(), checkGroupId);
        }
        return Result.success(null);
    }

    @Override
    public Result delete(Integer id) {
        setMealDao.delete(id);
        return Result.success(null);
    }

    @Override
    public Result update(Setmeal setmeal) {
        setMealDao.update(setmeal);
        setMealDao.deleteCheckGroupAndSetMeal(setmeal.getId());
        List<Integer> checkGroupIds = setmeal.getCheckGroupIds();
        for (Integer checkGroupId : checkGroupIds) {
            setMealDao.addSetMealAndCheckGroup(setmeal.getId(),checkGroupId);
        }
        return null;
    }

    @Override
    public Result mobileFindAll() {
        List<Setmeal> setmealList=setMealDao.mobileFindAll();
         List<Setmeal> collect = setmealList.stream().filter((setmeal) -> {
            setmeal.setImg("http://qyymngysq.hb-bkt.clouddn.com/" + setmeal.getImg());
            return true;
        }).collect(Collectors.toList());
        return Result.success(collect);
    }

    @Override
    public Result mobileFindByID(Integer id) {
        Setmeal setmeal= setMealDao.mobileFindByID(id);
        setmeal.setImg("http://qyymngysq.hb-bkt.clouddn.com/"+setmeal.getImg());
        return Result.success(setmeal);
    }


}
